package se.peterbjorkman.android.trafikkamera.infodownload;

import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.widget.Toast;

public class DownloadCameraInfoTask extends AsyncTask<String, DownloadProgress, String> implements ParserCallback {

	private Context mContext;
	private DownloadStatus mLastStatus = DownloadStatus.Preparing;
	private int mLastDone = 0;
	private int mLastTotal = 0;
	private ProgressDialog progressDialog;

	
	public DownloadCameraInfoTask(Context context){
		mContext = context;
	}
	
	@Override
	protected String doInBackground(String... params) {
		JSonDownloader downloader = new JSonDownloader();
		CameraJsonParser parser = new CameraJsonParser();
		JSONObject json = null;
		try {
			setStatus(DownloadStatus.Downloading);
			json = downloader.DownloadJSon(params[0]);
			setStatus(DownloadStatus.Inserting);
			parser.parseCameraJson(json, this);
			setStatus(DownloadStatus.Completed);
		} catch (DownloadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		}
		return null;
	}

	@Override
	public void HandleCamera(String id, String name, String description, String road,
			int county, double longitude, double latitude, String url,
			String handheldUrl) {
		CameraInserter.insertCamera(mContext, id, name, description, road, county, longitude, latitude, url, handheldUrl);
	}

	@Override
	public void SetParseProgress(int done, int total) {
		mLastDone = done;
		mLastTotal = total;
		reportProgress();
	}

	@Override
	protected void onProgressUpdate(DownloadProgress... changed) {
		//Log.v(LOGTAG, "We are making progress.");
		DownloadProgress progress = changed[0];
		int done = progress.getDone();
		int total = progress.getTotal();
		if (done > 0 && total > 0){
			progressDialog.setProgress(done);
			progressDialog.setMax(total);
		}
		String status = "Arbetar";
		switch (progress.getStatus()){
		case Downloading:
			status = "Hämtar data";
			break;
		case Parsing:
			status = "Tolkar data";
			break;
		case Inserting:
			status = "Behandlar kameror";
			break;
		}
		progressDialog.setMessage(status);
	}

	@Override
	protected void onPreExecute() {
		//Log.v(LOGTAG, "onPreExecute.");
		progressDialog = new ProgressDialog(mContext);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setMessage("Loading...");
		progressDialog.setCancelable(false);
		progressDialog.setMax(1);
		progressDialog.show();
	}

	@Override
	protected void onPostExecute(String errorString) {
		progressDialog.dismiss();
		if (errorString != null) {
			AlertDialog alertDialog;
			alertDialog = new AlertDialog.Builder(mContext).create();
			alertDialog.setTitle("Fel");
			alertDialog.setMessage(errorString);
			alertDialog.setButton("Close",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg, int sum) {
							// do nothing, close
						}
					});
			alertDialog.show();
		}
		else{
			Toast.makeText(mContext, "Uppdatering avslutad", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void setStatus(DownloadStatus status){
		mLastStatus = status;
		reportProgress();
	}
	
	private void reportProgress(){
		publishProgress(new DownloadProgress(mLastStatus, mLastDone, mLastTotal));
	}
}
